<!-- 
 * 修改密码
 * @author: 
 * @since: 2022-06-13
 * index.vue
-->
<template>
  <div class="edit-password-container">
    <div class="edit-password-box">
      <div class="title">修改密码</div>
      <el-form :model="form" label-width="75px" label-position="right" :rules="rules" ref="validateForm">
        <el-form-item label="旧密码:" prop="oldpassword">
          <el-input v-model="form.oldpassword" placeholder="请输入旧密码" clearable show-password type="password" />
        </el-form-item>
        <el-form-item label="设置密码:" prop="password">
          <el-input v-model="form.password" placeholder="请输入不多于20个字符" type="password" clearable show-password />
          <div class="tip">提示：密码应包含数字、大小写字母、特殊字符中的两种或两种以上</div>
        </el-form-item>
        <el-form-item label="确认密码:" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" placeholder="再次输入新密码" type="password" clearable show-password />
        </el-form-item>
      </el-form>
      <div class="handleBtn">
        <el-button type="primary" @click="submit">确定修改</el-button>
        <el-button @click="reset">取消修改</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, onMounted, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { editPasswordApi } from '@/api/interface/personCenter.js'
import { successHandle } from '@/utils/common.js'
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';
import md5 from 'js-md5';
const router = useRouter();
const store = useStore();

const form = reactive({
  oldpassword: '',//旧密码
  password: '',//新密码
  confirmPassword: '',//确定密码
})

//表单验证 
const rules = {
  oldpassword: [{ required: true, message: '必填项', trigger: 'blur', },
  { max: 20, message: '不能超出20个字符', trigger: 'blur' },],
  password: [{ required: true, message: '必填项', trigger: 'blur' },
  { max: 20, message: '不能超出20个字符', trigger: 'blur' },],
  confirmPassword: [{ required: true, message: '必填项', trigger: 'blur' },
  { max: 20, message: '不能超出20个字符', trigger: 'blur' },],
};

//form表单
const validateForm = ref(null)

//提交
const submit = () => {
  validateForm.value.validate((valid) => {
    if (valid) {
      if (form.password === form.confirmPassword) {
        if (form.password === form.oldpassword) {
          ElMessage({
            message: '旧密码和新密码必须不一致',
            type: 'warning',
          })
        } else {
          const md = "xx1WET12^%3^(WE45";
          const oldPwd = md5(md5(md) + md5(form.oldpassword))
          const pwd = md5(md5(md) + md5(form.password));
          const params = {
            oldpassword: oldPwd,
            password: pwd
          }
          editPasswordApi(params).then(res => {
            successHandle(res)
            const { status } = res
            if (status === 200) {
              store.dispatch('user/logOut').then((res) => {
                if (res) {
                  const { fullPath } = router.currentRoute._value;
                  router.push({ path: `/login?redirect=${fullPath}` }).catch(() => { });
                }
              });

            }

          })

        }
      } else {
        ElMessage({
          message: '两次新密码填写不一致',
          type: 'warning',
        })

      }


    } else {
      console.log('没通过验证');
    }
  });

}

//重置
const reset = () => {
  for (let key in form) {
    form[key] = ''
  }
  validateForm.value.resetFields()
}


</script>

<style lang="scss" scoped>
.edit-password-container {
  width: 100%;
  display: flex;
  justify-content: center;
  padding-top: 50px;
  box-sizing: border-box;
  .edit-password-box {
    width: 540px;
    border: 1px solid #e1e1e1;
    border-radius: 4px;
    display: flex;
    flex-direction: column;
    padding: 0 35px 20px;
    box-sizing: border-box;
    .title {
      color: #444;
      font-weight: bold;
      font-size: 18px;
      align-self: center;
      margin: 30px 0;
    }
    .tip {
      color: #333333;
      font-size: 12px;
    }
    .handleBtn {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}
</style>
